#ifndef __CHAIN_REGISTER_H__
#define __CHAIN_REGISTER_H__

#include "./includes/register_pack.h"
#include <cstring>

class ChainRegister : public Register{
	public:
		/* El bloque 0 se considera invalido */
		static const unsigned long int NULL_POINTER =  0;
	private:
		/* Parte del registro que ocupa varios bloques */
		char* data;
		unsigned int size;
		/* Siguiente bloque en la cadena */
		unsigned long int next;
		
	public:
		ChainRegister(char* data,unsigned int offset,unsigned int size,unsigned long int next){
			char* temp = data;
			this->data = new char[size];
			temp += offset;
			memcpy(this->data,temp,size);
			
			this->size = size;
			this->next = next;
		}
		
		ChainRegister(char* data,unsigned int size,unsigned long int next){
			this->data = new char[size];
			memcpy(this->data,data,size);			
			this->size = size;
			this->next = next;
		}
		
		ChainRegister(const ChainRegister& cr){
			this->data = new char[cr.size];
			memcpy(this->data,cr.data,size);			
			this->size = cr.size;
			this->next = next;
		}
		
		~ChainRegister(){
			delete[] this->data;
		}
		
		Register* duplicate() const{ return (new ChainRegister(*this)); }
		
		void setFields(const Register& b2){
			const ChainRegister& cr = dynamic_cast<const ChainRegister&>(b2);
			
			delete[] this->data;
			this->data = new char[cr.size];
			memcpy(this->data,cr.data,size);			
			this->size = cr.size;
		}
		
		bool operator<(const Register& b2)const{
			/* Estos registro NO son comparables */
			return true;
		}
				
		unsigned long int getNext()const{ return next; }
		
		const char* peekData()const{
			return data;
		}
		
		char* getData(){
			char* temp = new char[size];
			memcpy(temp,data,size);
			return  temp;
		}

		unsigned int getDataSize()const{
			return size;
		}
				
		static unsigned int getPointerSize(){
			return sizeof(unsigned int);
		}
				
		unsigned int getSize()const{
			return size+sizeof(unsigned long int);
		}
		
		std::ostream& toOstream(std::ostream& out)const{
			out << "\tEsta bloque contiene parte de un registro " << std::endl;
			out << "\tTamaño Particion : " << size << std::endl;
			return out;
		}		
};


#endif /* __CHAIN_REGISTER_H__ */
